package it.koutifaris.travianmap.tile.tilemap;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import it.koutifaris.travianmap.dal.RevisionService.Revision;
import it.koutifaris.travianmap.map.MapSquare;
import it.koutifaris.travianmap.tile.TileLoader;
import it.koutifaris.travianmap.tile.tilemap.TiledWorldMap;

import java.util.Date;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class TiledMapTest extends AndroidTestCase {

	private TiledWorldMap tiledMap;
	private TileLoader tileLoaderMockup;

	@Override
	public void setUp() {
		MapMockupFactory factory = new MapMockupFactory();
		tileLoaderMockup = factory.tileLoaderMockup();

		Revision revision = mock(Revision.class);
		when(revision.getId()).thenReturn(Long.valueOf(1));
		when(revision.getDay()).thenReturn(new Date());

		tiledMap = new TiledWorldMap(factory.mapLoaderMockup(tileLoaderMockup).getLoaderFor(revision), 6);
	}

	public void testGetRadius() {
		Assert.assertEquals(6, tiledMap.getRadius());
	}

	public void testResultInCache() {
		int x = -2;
		int y = -2;

		Assert.assertFalse(tiledMap.resulInCache(x, y));
		tiledMap.getSquareAt(x, y);
		Assert.assertTrue(tiledMap.resulInCache(x, y));
	}

	public void testLoaderCalledOnce() {
		int x = -2;
		int y = -2;

		tiledMap.getSquareAt(x, y);
		tiledMap.getSquareAt(x, y);

		verify(tileLoaderMockup, times(1)).loadTileAt(anyInt(), anyInt());
	}

	public void testLoaderCalledTwice() {
		tiledMap.getSquareAt(-1, -1);
		tiledMap.getSquareAt(0, 1);

		verify(tileLoaderMockup, times(2)).loadTileAt(anyInt(), anyInt());
	}

	public void testGetNorthWestVillage() {
		MapSquare northWestSquare = tiledMap.getSquareAt(-2, -2);
		Assert.assertNotNull(northWestSquare.getVillage());
	}

	public void testGetNorthEastVillage() {
		MapSquare northWestSquare = tiledMap.getSquareAt(-2, 1);
		Assert.assertNotNull(northWestSquare.getVillage());
	}

	public void testGetSouthWestVillage() {
		MapSquare northWestSquare = tiledMap.getSquareAt(1, 1);
		Assert.assertNotNull(northWestSquare.getVillage());
	}

	public void testGetSouthEastVillage() {
		MapSquare northWestSquare = tiledMap.getSquareAt(1, -2);
		Assert.assertNotNull(northWestSquare.getVillage());
	}

}
